<template>
    <!-- 编辑付费类型 -->
    <the-dialog :cfg="cfg" :loading="loading" :need-close-modal="false" @closed="onClose" @submited="onSubmit">
        <div class="popup-wrapper" slot="body">
            <div class="disease-tips">用于医保报销</div>
            <el-form class="x-form" ref="oForm" :model="form" :rules="rules" :disabled="loading" size="small" label-width="120px">
                <el-form-item label="病种名称" prop="diseaseName">
                    <el-select class="o-select--full" v-model="diseaseItem" placeholder="请选择" value-key="diseaseName" @change="handleChange">
                        <el-option v-for="(o,idx) in diseaseOptions" :key="idx" :label="o.diseaseName" :value="o">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </div>
    </the-dialog>
</template>

<script>
    export default {
        name: '',
        data() {
            return {
                cfg: {
                    visible: false,
                    title: '病种名称'
                },
                loading: false,

                patientInfo: {},

                form: {
                    diseaseName: '',
                    diseaseCode: '',
                },
                diseaseItem: {},
                rules: {
                    diseaseName: [{ required: true, message: '请选择病种', trigger: 'change' }]
                },
                diseaseOptions: [],
            }
        },
        methods: {
            onOpen(detail = {}) {
                this.cfg.visible = true;

                this.getDieaseList();

                for (let k in this.form) {
                    this.form[k] = detail[k] || '';
                    this.diseaseItem[k] = detail[k] || '';
                }
                this.$refs.oForm && this.$refs.oForm.clearValidate();
                this.patientInfo = Object.assign({}, detail);
            },
            getDieaseList() {
                this.loaidng = true;
                this.$ajax.getDiseaseOptionsByZh().then(res => {
                    this.loaidng = false;

                    this.diseaseOptions = res.data.list || [];
                })
            },
            handleChange(value) {
                for (let k in this.form) {
                    this.form[k] = value[k] || '';
                    this.diseaseItem[k] = value[k] || '';
                }
            },
            onSubmit() {
                this.loading = true;

                // if (!this.form.diseaseName) {
                //     this.loading = false;
                //     this.$message.closeAll();
                //     this.$message.error('请选择病种')
                //     return false;
                // }
                this.$refs.oForm.validate(valid => {
                    if (valid) {
                        let params = {
                            logId: sessionStorage.logId,
                            ...this.form
                        };

                        this.$ajax.updateYBDiseaseByZh(params).then(res => {
                            this.$message({
                                type: 'success',
                                message: res.msg
                            });

                            this.cfg.visible = false;
                            this.loading = false;

                            this.$emit('submited')
                        }).catch(err => {
                            this.loading = false;
                            this.$message.closeAll();
                            this.$message({
                                type: 'error',
                                message: err.msg
                            });
                        })
                    } else {
                        this.loading = false;
                    }
                })
            },
            onClose() {
                this.cfg.visible = false;

                Object.assign(this.$data, this.$options.data.call(this));
            }
        },
        mounted() {

        }
    }
</script>

<style lang="scss">
    .popup-wrapper {
        .disease-tips {
            margin: 6px;
            color: $--type-error;
            font-size: 12px;
            text-align: center
        }
    }
</style>